from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render

# Create your views here.
from django.urls import reverse
from oauth2_provider.views import ApplicationList, ApplicationUpdate, ApplicationRegistration, ApplicationDelete

from management.models import Oauth2ProviderApplication
from user.models import Oauth2ProviderUser

#
# @login_required(login_url='/user/login')
# def homepage(request):
# 	dataParam = {'username': request.user.username, 'company_name': '', 'user_reg_type': 0, 'applications': []}
# 	try:
# 		username = request.user.username
# 		_oauth_user_model = Oauth2ProviderUser.objects.get(username=username)
# 		dataParam['company_name'] = "22"
# 		dataParam['user_reg_type'] = _oauth_user_model.get('type', 0)  # 用户注册类型，0表示个人用户，1表示企业用户
# 		print(dataParam)
# 	except Exception as e:
# 		print(e)
# 		pass
# 	return render(request, 'management/homepage.html', dataParam)


class ManagementList(ApplicationList):
	template_name = 'management/homepage.html'

	def get(self, request, *args, **kwargs):
		dataParam = {'username': request.user.username, 'company_name': '', 'user_reg_type': 0, 'applications': []}
		try:
			username = request.user.username
			_oauth_user_model = Oauth2ProviderUser.objects.get(username=username)
			dataParam['company_name'] = ""
			dataParam['user_reg_type'] = _oauth_user_model.type  # 用户注册类型，0表示个人用户，1表示企业用户
			#print(dataParam)

			# applications
			applications = Oauth2ProviderApplication.objects.filter(user_id=request.user.id)
			dataParam['applications'] = applications
		except Exception as e:
			print(e)
			pass
		#dataParam['errno'] = 0
		#dataParam['message'] = "success"
		return render(request, 'management/homepage.html', dataParam)


class ManagementRegistration(ApplicationRegistration):
	def get_success_url(self):
		return reverse("management_homepage")


class ManagementUpdate(ApplicationUpdate):

	def get_success_url(self):
		return reverse("management_homepage")


class ManagementDelete(ApplicationDelete):
	def get_success_url(self):
		return reverse("management_homepage")